//app.js
//var apiHost = 'http://develop.jhjvip.cn:8100/hpp/'; //外部接口
// var apiHost = 'http://192.168.1.131:8081/mall/'; //外部接口
 var apiHost = 'https://xcx.uhuijia.com.cn/hpp/'
//var apiHost = 'http://47.92.67.233:8080/mall/hpp/'; //外部接口
var apiUpLoad = 'https://www.uhuijia.com.cn/';      //上传地址

var Appid = 'wx0d2c39ed5fab1f51';                   //appid
var Secret = 'b15fe76b7a982723fbc93c8960b8c644';    //Secret

App({
  onLaunch: function () {

    var that = this;
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())

    wx.setStorageSync('apiHost', apiHost);
    wx.setStorageSync('apiUpLoad', apiUpLoad);

    wx.setStorageSync('Appid', Appid);
    wx.setStorageSync('Secret', Secret);


    //1、调用微信登录接口，获取code
    wx.login({
      success: function (r) {
        var code = r.code;//登录凭证
        if (code) {
          that.getUseInfo(code)   //获取用户信息
        } else {
          console.log('获取用户登录态失败！' + r.errMsg)
        }
      },
      fail: function () {
        console.log('登陆失败')
      }
    })
  },
  /**
   * 获取用户信息
   */
  getUseInfo: function (code) {
    var that = this;

    //2、调用获取用户信息接口
    wx.getUserInfo({
      success: function (res) {
        that.VERBurl(res, code)   //解密用户登录完成后的url
      },
      fail: function (res) {
        console.log('获取用户信息失败')
      }
    })
  },
  /**
   * 解密用户登录完成后的url
   */
  VERBurl: function (res, code) {
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });
    //3.请求自己的服务器，解密用户信息 获取unionId等加密信息  (注意：这个东西必须需要 开放平台 授权)
    wx.request({
      url: apiHost + 'hpp/UserInfo/decodeUserInfo',//自己的服务接口地址
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        encryptedData: res.encryptedData,
        iv: res.iv,
        code: code,
        Appid: Appid,
        Secret: Secret
      },
      success: function (data) {
        //4.解密成功后 获取自己服务器返回的结果
        if (data.data.status == 1) {
          var userInfo_ = data.data.userInfo;
          var openIdFl = userInfo_.openId;
          var phone = userInfo_.phone;

          that.globalData.openId = openIdFl;
          that.getuserId(userInfo_.openId);//获取userId

          // wx.setStorageSync('userId', userInfo_.unionId);
          wx.setStorageSync('phone', phone);
          wx.setStorageSync('openId', userInfo_.openId);
          wx.setStorageSync('session_token', userInfo_.session_key);
          wx.setStorageSync('userInfo', userInfo_);
        } else {
          console.log('解密失败')
        }
        wx.hideLoading();   //隐藏加载loading
      },
      fail: function () {
        wx.hideLoading();   //隐藏加载loading
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 5000,
          mask: true
        });
      }
    })
  },

  /**
   * 获取userId
   */
  getuserId : function(openId){
    wx.showLoading({
      title: '加载中',
      icon : "loading",
      mask:true
    });

    wx.request({
      url: apiHost + 'hpp/user/userInfoByOpenId',
      method: 'GET',
      header : {
        "content-type" :'application/json'
      },
      data :{
        openId : openId
      },
      success :function(res){
        var code = res.data.code;
        var msg = res.data.msg;
        wx.hideLoading();   //隐藏加载loading
        if("10000"==code){
          var userId = res.data.data.id;
          wx.setStorageSync("userId", userId)
        }else{
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 5000,
            mask: true
          });
        }
      },
      fail : function(){
        wx.hideLoading();   //隐藏加载loading
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 5000,
          mask: true
        });
      }
    })
  },

  globalData: {
    openId: '',
    onemoreFlag: false
  }
})